package com.hazy.animalface.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hazy.animalface.R;
import com.hazy.animalface.module.Sticker;
import com.hazy.animalface.view.StickersActivity;

import java.util.ArrayList;
import java.util.List;

import me.iwf.photopicker.PhotoPreview;

/**
 * Created by Jiaui on 2017/4/21 0021.
 * <p/>
 * 头贴包详情适配器
 */
public class StickersAdapter extends RecyclerView.Adapter<StickersAdapter.MyViewHodler> {

    private Context context;
    private List<Sticker> stickers;
    private ArrayList<String> stickersUrlList;
    private boolean isEdit;

    public StickersAdapter(Context context, List<Sticker> stickers) {
        this.context = context;
        this.stickers = stickers;
        stickersUrlList = new ArrayList<>();
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    public void setStickersUrlList(ArrayList<String> stickersUrlList) {
        this.stickersUrlList = stickersUrlList;
        if (this.stickersUrlList == null) {
            this.stickersUrlList = new ArrayList<>();
        }
    }

    @Override
    public StickersAdapter.MyViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHodler(LayoutInflater.from(context).inflate(R.layout.item_stickers, parent, false));
    }

    @Override
    public void onBindViewHolder(StickersAdapter.MyViewHodler holder, final int position) {

        final Sticker sticker = stickers.get(position);
        if (sticker != null) {
            holder.mStickerImage.setImageURI(sticker.getSticker().getThumbnailUrl(true, 200, 200));
            holder.mStickerImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoPreview.builder().setPhotos(stickersUrlList).setCurrentItem(position)
                            .setShowDeleteButton(false).start((StickersActivity) context);
                }
            });
//            Glide.with(context)
//                    .load(sticker.getSticker().getThumbnailUrl(true, 100, 100))
//                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
//                    .placeholder(R.mipmap.ic_launcher)
//                    .error(R.mipmap.ic_launcher)
//                    .into(holder.mStickerImage);
        }

    }

    @Override
    public int getItemCount() {
        return stickers == null ? 0 : isEdit ? stickers.size() + 1 : stickers.size();
    }

    public class MyViewHodler extends RecyclerView.ViewHolder {

        SimpleDraweeView mStickerImage;
        View mDelteView;

        public MyViewHodler(View itemView) {
            super(itemView);
            mStickerImage = (SimpleDraweeView) itemView.findViewById(R.id.item_sticker_iv);
            mDelteView = itemView.findViewById(R.id.item_sticker_delete_btn);
        }
    }
}
